import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _wrapNativeSuper from "@babel/runtime/helpers/esm/wrapNativeSuper";
import { autorun } from 'mobx';
import { useEffect, useMemo, useState } from 'react';

var TimedMap = /*#__PURE__*/function (_Map) {
  _inherits(TimedMap, _Map);

  var _super = _createSuper(TimedMap);

  function TimedMap() {
    _classCallCheck(this, TimedMap);

    return _super.apply(this, arguments);
  }

  return _createClass(TimedMap);
}( /*#__PURE__*/_wrapNativeSuper(Map));

var DataSetStore = new TimedMap();
export var useDataSet = function useDataSet(dataSetFactory) {
  var cacheKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  var ds = useMemo(function () {
    if (cacheKey) {
      var cacheDS = DataSetStore.get(cacheKey);

      if (!cacheDS) {
        cacheDS = dataSetFactory();
        DataSetStore.set(cacheKey, cacheDS);
      }

      return cacheDS;
    }

    return dataSetFactory();
  }, []);
  return ds;
};
export var useDataSetCurrentPage = function useDataSetCurrentPage(ds) {
  var _useState = useState(ds.currentPage),
      _useState2 = _slicedToArray(_useState, 2),
      currentPage = _useState2[0],
      setCurrentPage = _useState2[1];

  useEffect(function () {
    var disposer = autorun(function () {
      setCurrentPage(ds.currentPage);
    });
    return disposer;
  }, []);
  return currentPage;
};
export var useDataSetIsSelected = function useDataSetIsSelected(ds) {
  var isSelectedFun = function isSelectedFun() {
    return ds.selected.length > 0;
  };

  var _useState3 = useState(isSelectedFun()),
      _useState4 = _slicedToArray(_useState3, 2),
      isSelected = _useState4[0],
      setIsSelected = _useState4[1];

  useEffect(function () {
    var disposer = autorun(function () {
      setIsSelected(isSelectedFun());
    });
    return disposer;
  }, []);
  return isSelected;
};
export var useDataSetEvent = function useDataSetEvent(ds, eventName, eventListener) {
  useEffect(function () {
    ds.addEventListener(eventName, eventListener);
    return function () {
      ds.removeEventListener(eventName, eventListener);
    };
  }, []);
};